package com.gljx.web.service.impl;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.gljx.util.Axamine;
import com.gljx.util.ServerResponse;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.gljx.util.BigDecimalFormat;
import com.gljx.util.GetFid;
import com.gljx.web.Entity.OtherPayDemand;
import com.gljx.web.Entity.OtherPayDemandEntry;
import com.gljx.web.Entity.User;
import com.gljx.web.dao.CommonDao;
import com.gljx.web.dao.OtherPayDemandEntryMapper;
import com.gljx.web.dao.OtherPayDemandMapper;
import com.gljx.web.service.OtherPayDemandService;

@Service
@Transactional
public class OtherPayDemandServiceImpl extends CommonDao implements OtherPayDemandService{
	
	@Autowired
	private OtherPayDemandEntryMapper otherPayDemandEntryMapper;
	
	@Autowired
	private OtherPayDemandMapper otherPayDemandMapper;
	
	/**
	 *取分页数据    dmz 20160921
	 *@param pindex 页码
	 *@param psql SQL语句，需要带 rownum rn 例如：select rownum rn,t.* from zzda t
	 *@param psize 每页记录数
	 * */
	@Override
	public Map<String, Object> selectPage(int pindex, String psql, int psize) {
		  Map<String, Object> paramMap = new HashMap<String, Object>();
	       paramMap.put("Pindex", pindex);
	       paramMap.put("Psql", psql);
	       paramMap.put("Psize", psize);
	       paramMap.put("Pcount", oracle.jdbc.OracleTypes.INTEGER);
	       paramMap.put("v_cur", oracle.jdbc.OracleTypes.CURSOR);
	       this.getSqlSession().select("com.gljx.web.dao.OtherPayDemandMapper.pagemap", paramMap, null);
	       return  paramMap;
	}

	/**
	 * 取记录总数      dmz 20160921
	 * @param psql   SQL语句
	 * @return
	 */
	@Override
	public Map<String, Object> selectPageCount(String psql) {
		   Map<String, Object> paramMap = new HashMap<String, Object>();
	       paramMap.put("Psql", psql);
	       paramMap.put("Pcount", oracle.jdbc.OracleTypes.INTEGER);
	       this.getSqlSession().select("com.gljx.web.dao.CarMapper.pagecount", paramMap, null);
	       return paramMap;
	}

	/**
	 * 根据条件查询子类信息
	 */
	@Override
	public List<OtherPayDemandEntry> queryOtherPayDemandEntry(String fparentId, String studentName, String payTimeFid) {
		// TODO Auto-generated method stub
		Map<String, Object> paramMap = new HashMap<String, Object>();
		paramMap.put("v_fparentid", fparentId);
		paramMap.put("v_studentName", studentName);
		paramMap.put("v_payTimeFid", payTimeFid);
		paramMap.put("v_cursor", oracle.jdbc.OracleTypes.CURSOR);
		this.getSqlSession().select("com.gljx.web.dao.OtherPayDemandEntryMapper.selectOtherPayDemandEntry", paramMap, null);
		@SuppressWarnings("unchecked")
		List<OtherPayDemandEntry> otherPayDemandEntrys = (List<OtherPayDemandEntry>) paramMap.get("v_cursor");
		return otherPayDemandEntrys;
	}

	/**
	 * 添加或修改
	 */
	@Override
	public void editTrainingCharge(OtherPayDemand otherPayDemand, String[] fseq, String[] cfstudentname,
			String[] cfstudentid, String[] cfpayitemid, String[] cfshuaka, String[] cfxianjin, String[] cfamount,
			String[] cfdesc, User user, String time) {
		// TODO Auto-generated method stub
		if("".equals(otherPayDemand.getFid()) || otherPayDemand.getFid() == null){
			//创建人id
			otherPayDemand.setFcreatorid(user.getFid());
			//利用UUID生成fid
			otherPayDemand.setFid(GetFid.UUIDIntoFid());
			//当前时间
			otherPayDemand.setFcreatetime(time);
			//状态
            otherPayDemand.setCfbillstate(3L);
            //添加
			otherPayDemandMapper.insertSelective(otherPayDemand);
		}else{
			//修改时间时间
			otherPayDemand.setFlastupdatetime(time);
			//修改人id
			otherPayDemand.setFlastupdateuserid(user.getFid());
			//修改
			otherPayDemandMapper.updateByPrimaryKeySelective(otherPayDemand);
			//先删除子类信息
			otherPayDemandEntryMapper.deleteByFparentId(otherPayDemand.getFid());
		}
		//添加子类信息
		if(fseq == null || fseq.length == 0){
			
		}else{
			for (int i = 0; i < fseq.length; i++) {
				OtherPayDemandEntry otherPayDemandEntry = new OtherPayDemandEntry();
				otherPayDemandEntry.setCfstudentname(cfstudentname[i]);
				otherPayDemandEntry.setCfstudentid(cfstudentid[i]);
				otherPayDemandEntry.setCfpayitemid(cfpayitemid[i]);
				if("".equals(cfshuaka[i])){
					
				}else{
					otherPayDemandEntry.setCfshuaka(BigDecimalFormat.formot(cfshuaka[i]));
				}
				if("".equals(cfxianjin[i])){
					
				}else{
					otherPayDemandEntry.setCfxianjin(BigDecimalFormat.formot(cfxianjin[i]));
				}
				if("".equals(cfamount[i])){
					
				}else{
					otherPayDemandEntry.setCfamount(BigDecimalFormat.formot(cfamount[i]));
				}
				otherPayDemandEntry.setCfdesc(cfdesc[i]);
				otherPayDemandEntry.setFseq((long)i+1);
				otherPayDemandEntry.setFid(GetFid.UUIDIntoFid());
				otherPayDemandEntry.setFparentid(otherPayDemand.getFid());
				otherPayDemandEntryMapper.insertSelective(otherPayDemandEntry);
			}
		}
	}

	/**
	 * 根据fid查询
	 */
	@Override
	public OtherPayDemand queryOtherDemandByFid(String fid) {
		// TODO Auto-generated method stub
		return otherPayDemandMapper.selectOtherDemandByFid(fid);
	}

	/**
	 * 根据fid删除
	 */
	@Override
	public void deleteOtherDemandByFid(String[] deleteId, String fid) {
		// TODO Auto-generated method stub
		if(fid == null || "".equals(fid)){
			for (int i = 0; i < deleteId.length; i++) {
				//先删除子类信息
				otherPayDemandEntryMapper.deleteByFparentId(deleteId[i]);
				//在删除父类信息
				otherPayDemandMapper.deleteByPrimaryKey(deleteId[i]);
			}
		}else{
			//先删除子类信息
			otherPayDemandEntryMapper.deleteByFparentId(fid);
			//在删除父类信息
			otherPayDemandMapper.deleteByPrimaryKey(fid);
		}
	}

	/**
	 * 审核，反审核
	 */
	@Override
    public int auditOtherPayDemand(String fid, String type, String fhandlerId, String message, String state, String fnumber) {
        // TODO Auto-generated method stub
		OtherPayDemand otherPayDemand = new OtherPayDemand();
        if ("1".equals(type)) {
            otherPayDemand.setCfbillstate(5L);
            Axamine axamine = new Axamine();
            ServerResponse serverResponse = axamine.auditProcess(fnumber, state, message);
            if (serverResponse.getStatus() == 1) {
                //fid
                otherPayDemand.setFid(fid);
                //审核人
                otherPayDemand.setFauditorid(fhandlerId);
                //修改人
                otherPayDemand.setFlastupdateuserid(fhandlerId);
                //结束
                return otherPayDemandMapper.updateByPrimaryKeySelective(otherPayDemand);
            } else if (serverResponse.getStatus() == 2) {
                //推进
                if (!"1".equals(state)) {
                    //fid
                    otherPayDemand.setFid(fid);
                    otherPayDemand.setCfbillstate(3L);
                    otherPayDemandMapper.updateByPrimaryKeySelective(otherPayDemand);
                }
                return 2;
            } else {
                //错误
                return 0;
            }
        }else{
            Axamine axamine = new Axamine();
            axamine.againstAuditProcess(fnumber, message);
            otherPayDemand.setCfbillstate(3L);
            otherPayDemand.setFid(fid);
            return otherPayDemandMapper.updateByPrimaryKeySelective(otherPayDemand);
        }
    }

    @Override
    public int modifyOtherPayDemand(OtherPayDemand otherPayDemand) {
        return otherPayDemandMapper.updateByPrimaryKeySelective(otherPayDemand);
    }

    @Override
    public List<OtherPayDemandEntry> selectOtherDemandByInfo(String fcompanyid, String studentName, String cfpayitemid, String beginTime, String endTime) {
        Map<String, String> map = new HashMap<>();
        map.put("studentName", studentName);
        map.put("cfpayitemid", cfpayitemid);
        map.put("beginTime", beginTime);
        map.put("endTime", endTime);
        map.put("fcompanyid", fcompanyid);
        return otherPayDemandEntryMapper.selectOtherDemandByInfo(map);
    }
}
